package gui;

import gui.screens.ErrorScreen;
import gui.screens.ExitScreen;
import gui.screens.LoginScreen;
import gui.screens.RBCanvas;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Displayable;

/**
 * Class that knows the midlet.
 * @author Sandra Mayer
 *
 */
public class CanvasManager {
	private static RabbitBuster midlet;
	private static Displayable curScreen;
	private static Displayable oldScreen;
	private static RBCanvas canvas;
	
	/**
	 * Private Constructor.
	 *
	 */
	private CanvasManager() {
		//Do nothing
	}
	
	/**
	 * Gets the midlet.
	 * @return the midlet
	 */
	public static RabbitBuster getMidlet() {
		return midlet;
	}
	
	/**
	 * Sets the midlet.
	 * @param midlet the midlet
	 */
	public static void setMidlet(RabbitBuster midlet) {
		CanvasManager.midlet = midlet;
	}
	
	/**
	 * Gets the old screen.
	 * @return the old screen
	 */
	public static Displayable getOldScreen() {
		return oldScreen;
	}
	
	/**
	 * Sets the old screen.
	 * @param oldScreen the old screen
	 */
	public static void setOldScreen(Displayable oldScreen) {
		CanvasManager.oldScreen = oldScreen;
	}
	
	/**
	 * Sets the displayable on the display.
	 * @param display the displayable
	 */
	public static void setCurrent(Displayable display) {
		midlet.getDisplay().setCurrent(display);
	}
	
	/**
	 * Shows the old screen.
	 */
	public static void showOldScreen() {
		midlet.getDisplay().setCurrent(oldScreen);
		setCurScreen(oldScreen);
	}
	
	/**
	 * Destroys the application.
	 */
	public static void destroyApp() {
		midlet.destroyApp(false);
	}

	/**
	 * Sets the alarm screen.
	 * @param a alarm screen
	 * @param l screen to show
	 */
	public static void setCurrent(Alert a, LoginScreen l) {
		midlet.getDisplay().setCurrent(a, l);
	}
	
	/**
	 * Shows the exit screen.
	 */
	public static void showExitScreen() {
		midlet.getDisplay().setCurrent(ExitScreen.getInstance());
	}

	/**
	 * Gets the current screen.
	 * @return the current screen
	 */
	public static Displayable getCurScreen() {
		return curScreen;
	}

	/**
	 * Sets the current screen.
	 * @param curScreen the current screen
	 */
	public static void setCurScreen(Displayable curScreen) {
		CanvasManager.oldScreen = CanvasManager.curScreen;
		CanvasManager.curScreen = curScreen;
	}
	
	/**
	 * Shows the error screen.
	 * @param e the exception to show
	 */
	public static void showErrorScreen(Exception e) {
		midlet.getDisplay().setCurrent(ErrorScreen.getInstance(e));
	}
	
	/**
	 * Registers the the game field to the midlet.
	 * @param canvas the game field
	 */
	public static void setRBCanvasToMidlet(RBCanvas canvas) {
		midlet.setRBCanvas(canvas);
		CanvasManager.canvas = canvas;
	}
	
	/**
	 * Shows the game field.
	 */
	public static void showCanvas() {
		midlet.getDisplay().setCurrent(canvas);
	}
	
	/**
	 * Shows the current screen again.
	 */
	public static void showCurScreen() {
		midlet.getDisplay().setCurrent(CanvasManager.curScreen);
	}
}
